<template>
    <div id="navbarTag">
        <el-tag
        
        v-for="tag in TagList"
        :key="tag.name"
        closable
        :type=" tag.active == 1 ? 'success' : '' "
        :disable-transitions="false"
        @close="handleClose(tag)">
        {{ tag.name }}
        </el-tag>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                TagList: []
            }
        },
        mounted () {
            this.getTag();
        },
        computed: {
            path() {
                return this.$route 
            }
        },
        watch: {
            path() {
                this.getTag()
            }
        },
        methods: {
            // 跳转路由后保存路由信息
            getTag() {
                let pathName = this.$route.name
                if (pathName) {
                   let hasEle = this.TagList.find( el => {
                       return el.name == pathName
                    })
                    console.log(hasEle);
                    
                   if (hasEle) {
                       this.TagList.forEach( el => {
                           if (el.name == pathName) {
                               el.active = 1
                           } else {
                               el.active = 2
                           }
                       })
                   } else {
                       this.TagList.forEach(el => {
                           el.active = "2"
                       })
                       this.TagList.push({
                           name: pathName,
                           active: "1"
                       })
                   }
                    
                }
            },
            // 关闭 tag
            handleClose(tag) {
                let inx = ""
                this.TagList.forEach( (el, index) => {
                    if (el.name == tag.name) {
                        inx = index
                    }
                })
                this.TagList.splice(inx, 1)
            },
        },
    }
</script>

<style lang="scss" scoped>
    #navbarTag {
        width: 100%;
        box-sizing: border-box;
        border-bottom: 1px solid #f5f5f5;
        padding: 10px 0;
        display: flex;
        .el-tag {
            margin: 0 3px;
        }
    }
</style>